#ifndef SQL_H
#define SQL_H

#include <QObject>
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <QtGlobal>
#include <QTime>
#include <QVector>
class SQL : public QObject
{
    Q_OBJECT
public:
    static SQL* gethand()
    {
        if(sqlhand == nullptr)
            sqlhand = new SQL();
        return sqlhand;
    }

    /*
     * @brief 初始化数据库，在每次重启服务器时都要首先执行，当数据库中的表单已经存在时不会重新创建表单
     */
    void initDatabase();

    QString registerAccount(QString password, QString phone_num, QString nick_name = "");
    QString getPhoneNumber(QString account);
    void    removeAccount(QString account);
    void    addAccountIP(QString account, QString ip);
    void    removeAccountIP(QString account, QString ip);
    void    removeGroup(QString group_id);
    bool    checkAccountIP(QString account, QString ip);
    bool    verifyLogin(QString account, QString password);
    QString LoginIn(QString account, QString password, QString ip, bool mode = true);
    QString newGroup(QString account, int is_group, QString group_name = "");
    bool    getIsGroup(QString group_id);
    void    addOneToGroup(QString group_id, QString account);
    void    kickOneFromGroup(QString group_id, QString account);
    void    saveHistory(QString group_id, QString news, QString time);
    void    getHistory(QString group_id, QVector<QString> &history_list,int num = -1);
    void    getGroupIdOnAccount(QString account, QVector<QString> &group_id_list);
    void    getAccountFromGroup(QString group_id, QVector<QString> &account_list);
    void    addFriendApplication(QString account, QString Info);
    void    removeFriendApplication(QString account, QString Info);
    void    getFriendApplication(QString account, QVector<QString> &friend_application_list);
    QString getGroupLeaderID(QString group_id);
    QString getNickNameFromAccount(QString account);
    QString getGroupNameFromGroupid(QString group_id);
    void    updatePassword(QString account, QString new_pwd);
    void    updateNickName(QString account, QString new_name);
    void    updateGroupName(QString group_id, QString new_name);
    void    addFriend(QString account_1, QString account_2);
private:
    void createRegInfoTable();
    void createAccountIPTable();
    void createGroupChatTable();
    void createGroupChatInfoTable();
    void createHisRecordsTable();
    void createFriendApplicationTable();

    //用于向不同的表中插入信息，每次插入一条信息
    void insertRegInfoTable(QString _Account, QString _Password, QString _PhoneNumber, QString _NickName = "");
    void insertAccountIPTable(QString _Account, QString _IP);
    void insertGroupChatTable(QString _GroupNumber, int _IsGroup, QString _GroupLeaderID = "", QString _GroupName = "");
    void insertGroupChatInfoTable(QString _GroupNumber, QString _Account);
    void insertHisRecordsTable(QString _GroupNumber, QString _Time, QString _News);
    void insertFriendApplication(QString _Account, QString _ApplicationInfo);

    //用于删除表中的元组，每次删除一个元组
    void deleteRegInfoTable(QString _Account);
    void deleteAccountIPTable(QString _Account, QString _IP);
    void deleteGroupChatTable(QString _GroupNumber);
    void deleteGroupChatInfoTable(QString _GroupNumber, QString _Account);
    void deleteHisRecordsTable(QString _GroupNumber, QString _Time);
    void deleteFriendApplication(QString _Account, QString _ApplicationInfo);

    //用于更新表中的元组，每次更新一个元组
//    void updateRegInfoTable(QString _Account, QString _Password, QString _PhoneNumber, QString _NickName = "");
//    void updateAccountIPTable(QString _Account, QString _IP);
//    void updateGroupChatTable(QString _GroupNumber, QString _GroupName = "");
//    void updateGroupChatInfoTable(QString _GroupNumber, QString _Account);
//    void updateHisRecordsTable(QString _GroupNumber, QString _Time, QString _News);

    //用于删除表中的含有相同键值的多个元组
    void deleteAccountIPTableOnAccount(QString _Account);
    void deleteGroupChatInfoTableOnGroupId(QString _GroupNumber);
    void deleteGroupChatInfoTableOnAccount(QString _Account);
    void deleteHisRecordsTableOnGroupId(QString _GroupNumber);
    void deleteFriendApplicationOnAccount(QString _Account);
private:
    explicit SQL(QObject *parent = nullptr);
    static SQL* sqlhand;
    QSqlDatabase database;

signals:

};

#endif // SQL_H
